package com.sync.data.job.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.sync.data.ievent.entity.IeventBaseEmployee;
import com.sync.data.ievent.repository.IeventBaseEmployeeRepository;
import com.sync.data.mce.entity.MceBaseEmployee;
import com.sync.data.mce.repository.MceBaseEmployeeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.Collections;
import java.util.Date;

@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class EmployeeSyncBatchJob {


    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    private final MceBaseEmployeeRepository mceBaseEmployeeRepository;

    private final IeventBaseEmployeeRepository ieventBaseEmployeeRepository;

    private final PlatformTransactionManager transactionManagerIevent;

    private final JpaItemWriter<IeventBaseEmployee> employeeJpaItemWriter;

    // 批处理大小
    private static final int CHUNK_SIZE = 100;

//    @Bean
//    public Job employeeSyncJob() {
//        return jobBuilderFactory.get("employeeSyncJob")
//                .incrementer(new RunIdIncrementer())
//                .start(syncDepartmentStep())
//                .build();
//    }

    @Bean
    public Step syncEmployeeStep() {
        return stepBuilderFactory.get("syncEmployeeStep")
                .<MceBaseEmployee, IeventBaseEmployee>chunk(CHUNK_SIZE)
                .reader(mceBaseEmployeeItemReader())
                .processor(employeeSyncProcessor())
                .writer(employeeJpaItemWriter)
                .transactionManager(transactionManagerIevent)
                .build();
    }

    public ItemReader<MceBaseEmployee> mceBaseEmployeeItemReader() {
        RepositoryItemReader<MceBaseEmployee> reader = new RepositoryItemReader<>();
        reader.setRepository(mceBaseEmployeeRepository);
        reader.setMethodName("findAll");
        reader.setSort(Collections.singletonMap("id", Sort.Direction.ASC));
        reader.setPageSize(CHUNK_SIZE);
        return reader;
    }

    public ItemProcessor<MceBaseEmployee, IeventBaseEmployee> employeeSyncProcessor() {
        return mceBasePositionEmployee -> {
            IeventBaseEmployee ieventBaseEmployee  = ieventBaseEmployeeRepository.findById(mceBasePositionEmployee.getId()).orElse(null);

            if (ieventBaseEmployee == null){
                ieventBaseEmployee = new IeventBaseEmployee();
                ieventBaseEmployee.setCreateTime(new Date());
            }
            BeanUtil.copyProperties(mceBasePositionEmployee, ieventBaseEmployee , CopyOptions.create().ignoreNullValue().ignoreError().setIgnoreProperties("updateTime","createTime"));
            ieventBaseEmployee.setDelFlag("0");
            ieventBaseEmployee.setCreateTime(new Date());
            return ieventBaseEmployee;
        };
    }
}